<template>
  <div>
    <el-table
      :data="bannerLists"
      style="width: 100%;"
    >
      <el-table-column
        label="序号"
        width="180"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="类别"
        width="180"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name == "b" ? '背景图片' : '轮播图片' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="缩略图"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px;"
            :src="baseUrl + scope.row.thumb"
            fit="fill"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="原图"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px;"
            :src="baseUrl + scope.row.imgUrl"
            fit="fill"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button @click="handle(scope.row, scope.$index)">编辑</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出框 -->
    <el-dialog title="轮播图信息" :visible.sync="dialogFormVisible">
      <el-form :model="item">
        <el-form-item label="类别" :label-width="formLabelWidth">
          <el-input v-model="item.name" autocomplete="off" disabled />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <span>缩略图</span>
            <Upload :img-url=" item.thumb" @upload="thumbUp" />
          </el-col>
          <el-col :span="12">
            <span>原图</span>
            <Upload :img-url="item.imgUrl" @upload="imgUrlUp" />
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { getbanner, upBanner } from '@/api/banner'
import { baseUrl } from '@/routerConfig'
import Upload from '@/components/uploade'
import { Message } from 'element-ui'
import { mapGetters } from 'vuex'
export default {
  components: {
    Upload
  },
  data() {
    return {
      bannerLists: [],
      baseUrl,
      dialogFormVisible: false,
      item: '',
      index: '',
      isUp: false
    }
  },
  async beforeCreate() {
    // const result = await getbanner()
    // this.bannerList.push(result.data.background)
    // this.bannerList.push(...result.data.banner)
    this.$store.dispatch('banner/getBannerList').then((res) => {
      this.bannerLists.push(...this.bannerList)
    })
  },
  // eslint-disable-next-line vue/order-in-components
  computed: {
    ...mapGetters(['bannerList']),
    list() {
      return this.bannerLists
    }
  },
  methods: {
    handle(e, index) {
      this.dialogFormVisible = true
      this.item = { ...e }
      this.index = index
    },
    thumbUp(e) {
      this.item.thumb = e
    },
    imgUrlUp(e) {
      this.item.imgUrl = e
    },
    async submit() {
      this.dialogFormVisible = false
      const temp = this.bannerList[this.index]
      for (const p in temp) {
        if (temp[p] !== this.item[p]) {
          this.isUp = true
          break
        }
      }
      if (this.isUp) {
        try {
          await this.$store.dispatch('banner/upBannerList', { ...this.item, index: this.index })
          this.isUp = false
          this.bannerLists[this.index] = this.item
          Message.success('修改成功')
        } catch (e) {
          Message.error(e)
          this.isUp = false
        }
      } else {
        Message.success('无需修改')
      }
    }
  }

}
</script>

<style>

</style>
